﻿


using MotionCS.Controller;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionCS.Enumerate;
using MotionCS.Helper;

namespace MotionCS.Module
{
    public class ProductData
    {
        /// <summary>
        /// 产品ID
        /// </summary>
        public string Uuid;
        /// <summary>
        /// 产品条码
        /// </summary>
        public string QrCode;
        /// <summary>
        /// 所属工位
        /// </summary>
        public int Station;
    }

    public class DataCenter
    {
        /// <summary>
        /// 单例锁对象
        /// </summary>
        private static object objLock = new object();
        /// <summary>
        /// 单例的实例
        /// </summary>
        private static DataCenter instance;
        public static DataCenter Instance
        {
            get
            {
                if (instance == null)
                {
                    lock(objLock)
                    {
                        if (instance == null)
                        {
                            instance = new DataCenter();
                        }
                    }
                }

                return instance;
            }
        }
        public enum ResetStatusE
        {
            /// <summary>
            /// 未复位
            /// </summary>
            None,
            /// <summary>
            /// 复位中
            /// </summary>
            Process,
            /// <summary>
            /// 复位完成
            /// </summary>
            Finish,
            /// <summary>
            /// 复位失败
            /// </summary>
            Failed,
        }

        private class ResetData
        {
            /// <summary>
            /// 所属工位
            /// </summary>
            public int Station;
            /// <summary>
            /// 是否完成
            /// </summary>
            public ResetStatusE Status = ResetStatusE.None;
        }

        /// <summary>
        /// 相机模组接收队列
        /// </summary>
        private ConcurrentDictionary<int, ConcurrentQueue<ProductData>> cameraProductData;
        /// <summary>
        /// 轨道模组发送队列
        /// </summary>
        private ConcurrentDictionary<int, ConcurrentQueue<ProductData>> conveyorProductData;
        /// <summary>
        /// 相机模组复位状态
        /// </summary>
        private ConcurrentBag<ResetData> resetCameras;
        /// <summary>
        /// 构造函数
        /// </summary>
        public DataCenter()
        {
            cameraProductData = new ConcurrentDictionary<int, ConcurrentQueue<ProductData>>();
            conveyorProductData = new ConcurrentDictionary<int, ConcurrentQueue<ProductData>>();
            resetCameras = new ConcurrentBag<ResetData>();
        }
        /// <summary>
        /// 添加产品信息到相机模组
        /// </summary>
        /// <param name="station"></param>
        /// <param name="productData"></param>
        public void CameraEnqueue(int station, ProductData productData)
        {
            if (!cameraProductData.ContainsKey(station))
            {
                cameraProductData.TryAdd(station, new ConcurrentQueue<ProductData>());
            }

            //while (cameraProductData[station].TryDequeue(out ProductData itm)) ;
            cameraProductData[station].Enqueue(productData);
        }
        /// <summary>
        /// 获取产品信息出相机模组
        /// </summary>
        /// <param name="camera"></param>
        /// <returns></returns>
        public bool CameraTryDequeue(int station, out ProductData productData)
        {
            if (cameraProductData.ContainsKey(station))
            {
                return cameraProductData[station].TryDequeue(out productData);
            }
            else
            {
                productData = null;
                return false;
            }
        }
        /// <summary>
        /// 清除相机模组产品信息
        /// </summary>
        /// <param name="station"></param>
        public void CameraProductDataClear(int station)
        {
            if (cameraProductData.ContainsKey(station))
            {
                while (cameraProductData[station].TryDequeue(out ProductData data)) ;
            }
        }
        /// <summary>
        /// 添加产品信息到轨道模组
        /// </summary>
        /// <param name="productData"></param>
        public void ConveyorEnqueue(int station, ProductData productData)
        {
            if (!conveyorProductData.ContainsKey(station))
            {
                conveyorProductData.TryAdd(station, new ConcurrentQueue<ProductData>());
            }

            //while (conveyorProductData[station].TryDequeue(out ProductData itm)) ;

            conveyorProductData[station].Enqueue(productData);
        }
        /// <summary>
        /// 获取产品信息出轨道模组
        /// </summary>
        /// <param name="conveyor"></param>
        /// <returns></returns>
        public bool ConveyorTryDequeue(int station, out ProductData productData)
        {
            if (conveyorProductData.ContainsKey(station))
            {
                return conveyorProductData[station].TryDequeue(out productData);
            }
            else
            {
                productData = null;
                return false;
            }
        }
        /// <summary>
        /// 清除轨道模组产品信息
        /// </summary>
        /// <param name="station"></param>
        public void ConveyorProductDataClear(int station)
        {
            if (conveyorProductData.ContainsKey(station))
            {
                while (conveyorProductData[station].TryDequeue(out ProductData data)) ;
            }
        }
        /// <summary>
        /// 相机复位初始化
        /// </summary>
        /// <param name="station"></param>
        public void CameraResetInit(int station)
        {
            resetCameras.Add(new ResetData() { Station = station, Status = ResetStatusE.Finish});
        }
        /// <summary>
        /// 设置相机复位未开始
        /// </summary>
        /// <param name="station"></param>
        public void SetCameraResetNone(int station)
        {
            resetCameras.ToList().Find(itm => itm.Station == station).Status = ResetStatusE.None;
        }
        /// <summary>
        /// 设置相机复位进行中
        /// </summary>
        /// <param name="station"></param>
        public void SetCameraResetProcess(int station)
        {
            resetCameras.ToList().Find(itm => itm.Station == station).Status = ResetStatusE.Process;
        }
        /// <summary>
        /// 设置相机复位完成
        /// </summary>
        /// <param name="station"></param>
        public void SetCameraResetFinish(int station)
        {
            resetCameras.ToList().Find(itm => itm.Station == station).Status = ResetStatusE.Finish;
        }
        /// <summary>
        /// 设置相机复位失败
        /// </summary>
        /// <param name="station"></param>
        public void SetCameraResetFailed(int station)
        {
            resetCameras.ToList().Find(itm => itm.Station == station).Status = ResetStatusE.Failed;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ResetStatusE GetCameraResetStatus()
        {
            if (resetCameras.ToList().Exists(itm => itm.Status == ResetStatusE.Failed))
            {
                return ResetStatusE.Failed;
            }
            else if (resetCameras.ToList().Exists(itm => itm.Status == ResetStatusE.Process))
            {
                return ResetStatusE.Process;
            }
            else
            {
                return ResetStatusE.Finish;
            }
        }
    }
}
